false, 'message' => '', 'application_no' => ''];
// Validate CSRF token
if (!validate_csrf_token($_POST['csrf_token'])) {
$response['message'] = "Security token validation failed. Please try again.";
echo json_encode($response);
exit();
}
// Validate and sanitize data
$first_name = sanitize_input($_POST['first_name']);
$middle_name = sanitize_input($_POST['middle_name']);
$last_name = sanitize_input($_POST['last_name']);
$date_of_birth = $_POST['date_of_birth'];
$present_address = sanitize_input($_POST['present_address']);
$home_town = sanitize_input($_POST['home_town']);
$lga = sanitize_input($_POST['lga']);
$religion = sanitize_input($_POST['religion']);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$last_school = sanitize_input($_POST['last_school']);
$last_class = sanitize_input($_POST['last_class']);
// Parent/Guardian data
$parent_name = sanitize_input($_POST['parent_name']);
$parent_phone = sanitize_input($_POST['parent_phone']);
$parent_occupation = sanitize_input($_POST['parent_occupation']);
$parent_address = sanitize_input($_POST['parent_address']);
// Basic validation
if (empty($first_name) || empty($last_name)) {
$response['message'] = "First name and last name are required.";
echo json_encode($response);
exit();
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$response['message'] = "Please provide a valid email address.";
echo json_encode($response);
exit();
}
if (empty($date_of_birth)) {
$response['message'] = "Date of birth is required.";
echo json_encode($response);
exit();
}
// Handle file upload if exists
$passport_photo = '';
if (isset($_FILES['passport_photo']) && $_FILES['passport_photo']['error'] === 0) {
$allowed_types = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'];
$max_size = 2 * 1024 * 1024;
$file_type = $_FILES['passport_photo']['type'];
$file_size = $_FILES['passport_photo']['size'];
if (in_array($file_type, $allowed_types) && $file_size <= $max_size) {
$upload_dir = 'uploads/passports/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0755, true);
}
$file_extension = pathinfo($_FILES['passport_photo']['name'], PATHINFO_EXTENSION);
$new_filename = 'passport_' . time() . '_' . uniqid() . '.' . $file_extension;
$upload_path = $upload_dir . $new_filename;
if (move_uploaded_file($_FILES['passport_photo']['tmp_name'], $upload_path)) {
$passport_photo = $upload_path;
}
}
}
try {
$application_no = generateApplicationNumber();
$stmt = $DBcon->prepare("INSERT INTO applications (
application_no, passport_photo, first_name, middle_name, last_name,
date_of_birth, present_address, home_town, lga, religion, email,
last_school, last_class, parent_name, parent_phone, parent_occupation,
parent_address
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([
$application_no,
$passport_photo,
$first_name,
$middle_name,
$last_name,
$date_of_birth,
$present_address,
$home_town,
$lga,
$religion,
$email,
$last_school,
$last_class,
$parent_name,
$parent_phone,
$parent_occupation,
$parent_address
]);
// Send email notifications
sendApplicationEmails($application_no, $first_name . ' ' . $last_name, $email, $parent_name);
$response['success'] = true;
$response['application_no'] = $application_no;
$response['message'] = "Application submitted successfully!";
} catch (PDOException $e) {
error_log("Application save error: " . $e->getMessage());
$response['message'] = "Failed to submit application. Please try again.";
}
echo json_encode($response);
exit();
}
// Function to send email notifications
function sendApplicationEmails($app_no, $student_name, $student_email, $parent_name) {
// School admin email
$admin_email = "info@mmgssnnewi.org";
$school_email = "mmgssnnewi@gmail.com";
// Email to student/parent
$to_student = $student_email;
$subject_student = "Application Submitted Successfully - Mother of Mercy Girls Secondary School";
$message_student = '
Application Confirmation
Dear ' . $student_name . ',
Thank you for submitting your application to Mother of Mercy Girls Secondary School, Nnewi.
Application Status: Submitted Successfully
Date: ' . date('F d, Y') . '
Reference: ' . $app_no . '
' . $app_no . '
Your application has been received and is currently under review. Please keep your application number safe as you will need it for future correspondence and to check your application status.
Next Steps:
Application review (2-3 business days)
Entrance examination notification
Interview schedule (if applicable)
Final admission decision
You will be contacted via email or phone with further instructions.
For inquiries:
Email: admissions@mmgss.edu.ng
Phone: +234 803 123 4567
';
// Email to admin
$subject_admin = "New Application Received - " . $app_no;
$message_admin = '
New Application Alert
New Student Application Received
Application Number: ' . $app_no . '
Received: ' . date('Y-m-d H:i:s') . '
Applicant Information:
Student Name: ' . $student_name . '
Parent/Guardian: ' . $parent_name . '
Application Date: ' . date('F d, Y') . '
Please review this application in the admin portal at your earliest convenience.
View Application Details
';
// Email headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: Mother of Mercy Girls SS " . "\r\n";
// Send emails
@mail($to_student, $subject_student, $message_student, $headers);
@mail($admin_email, $subject_admin, $message_admin, $headers);
@mail($school_email, $subject_admin, $message_admin, $headers);
}
?>
Online Application - Mother of Mercy Girls Secondary School Nnewi